package rs.etf.kdp.workstation;

import java.awt.EventQueue;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class WorkstationUI {

	private JFrame frame;
	private JTextField txtNumberOfThreads;
	private JLabel lblIPAddress;
	private JLabel lblHostName;
	private JLabel lblIpAddress;
	private JLabel lblServerIpAddress;
	private JTextField txtServerIP;
	private JLabel lblPort;
	private JTextField txtPort;
	static JLabel lblWorkstationStatus;
	private JTextArea txtStatus;
	private JTextField txtName;
	static JButton btnRunWorkstation;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		//Scanner console = new Scanner(System.in);
		//System.out.println("Do you want to use Graphical User Interface? yes/no");
		String usegui;
		//String usegui = console.next();
		usegui = "yes";
		if(usegui.equals("y") || usegui.equals("Y") || usegui.equals("Yes") || usegui.equals("yes") || usegui.equals("YES"))
		{
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						WorkstationUI window = new WorkstationUI();
						window.frame.setVisible(true);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		} else if(usegui.equals("n") || usegui.equals("N") || usegui.equals("No") || usegui.equals("no") || usegui.equals("NO"))
		{
			System.out.println("You have choosen not to use GUI");
		} else
		{
			return;
		}
	}

	/**
	 * Create the application.
	 */
	public WorkstationUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * @wbp.parser.entryPoint
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setResizable(false);
		frame.setBounds(100, 100, 454, 372);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblNumberOfThreads = new JLabel("Number of threads for simulation");
		lblNumberOfThreads.setBounds(10, 140, 188, 14);
		frame.getContentPane().add(lblNumberOfThreads);
		
		txtNumberOfThreads = new JTextField();
		txtNumberOfThreads.setText("4");
		txtNumberOfThreads.setBounds(208, 137, 37, 20);
		frame.getContentPane().add(txtNumberOfThreads);
		txtNumberOfThreads.setColumns(10);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel.setBounds(10, 11, 235, 77);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lblHostNamee = new JLabel("Host Name:");
		lblHostNamee.setBounds(10, 46, 77, 14);
		panel.add(lblHostNamee);
		
		lblIPAddress = new JLabel("New label");
		lblIPAddress.setBounds(92, 11, 156, 14);
		panel.add(lblIPAddress);
		
		lblHostName = new JLabel("New label");
		lblHostName.setBounds(92, 46, 156, 14);
		panel.add(lblHostName);
		
		lblIpAddress = new JLabel("IP Address");
		lblIpAddress.setBounds(10, 11, 72, 14);
		panel.add(lblIpAddress);
		
		lblServerIpAddress = new JLabel("Server IP Address");
		lblServerIpAddress.setBounds(10, 171, 126, 14);
		frame.getContentPane().add(lblServerIpAddress);
		
		txtServerIP = new JTextField();
		txtServerIP.setText("localhost");
		txtServerIP.setBounds(146, 168, 99, 20);
		frame.getContentPane().add(txtServerIP);
		txtServerIP.setColumns(10);
		
		lblPort = new JLabel("Port");
		lblPort.setBounds(10, 199, 46, 14);
		frame.getContentPane().add(lblPort);
		
		txtPort = new JTextField();
		txtPort.setText("4001");
		txtPort.setBounds(146, 196, 99, 20);
		frame.getContentPane().add(txtPort);
		txtPort.setColumns(10);
		
		btnRunWorkstation = new JButton("Run workstation");
		btnRunWorkstation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnRunClicked();
			}
		});
		btnRunWorkstation.setBounds(284, 11, 154, 23);
		frame.getContentPane().add(btnRunWorkstation);
		
		lblWorkstationStatus = new JLabel("New label");
		lblWorkstationStatus.setBounds(284, 65, 154, 14);
		frame.getContentPane().add(lblWorkstationStatus);
		setCaption();
		lblWorkstationStatus.setText("Workstation is not active");
		lblWorkstationStatus.setForeground(Color.RED);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 224, 428, 109);
		frame.getContentPane().add(scrollPane);
		
		txtStatus = new JTextArea();
		scrollPane.setViewportView(txtStatus);
		
		JLabel lblName = new JLabel("Name");
		lblName.setBounds(10, 115, 46, 14);
		frame.getContentPane().add(lblName);
		
		txtName = new JTextField();
		txtName.setText("Test");
		txtName.setBounds(146, 109, 99, 20);
		frame.getContentPane().add(txtName);
		txtName.setColumns(10);
	}
	
	private void setCaption()
	{
		String hostname = null;
		String ipAddress = null;
		try {
			InetAddress address = InetAddress.getLocalHost();
			hostname = address.getHostName();
			ipAddress = address.getHostAddress();
		} catch (UnknownHostException e) {
			hostname = "UNKNOWN";
		}
		
		frame.setTitle("Workstation: " + hostname);
		lblHostName.setText(hostname);
		lblIPAddress.setText(ipAddress);
	}
	
	public void btnRunClicked()
	{
		int numberOfThreads;
		String serverIP;
		int serverPort;
		String name;
		
		name = txtName.getText();
		if(name.equals(""))
		{
			JOptionPane.showMessageDialog(frame, "You must enter name of this workstation");
			return;
		}
		
		try {
			numberOfThreads = Integer.parseInt(txtNumberOfThreads.getText());
		}catch(NumberFormatException ne)
		{
			JOptionPane.showMessageDialog(frame, "You must eneter valid integer for number of threads");
			return;
		}
		
		serverIP = txtServerIP.getText();
		try {
			serverPort = Integer.parseInt(txtPort.getText());
		}catch(NumberFormatException ne)
		{
			JOptionPane.showMessageDialog(frame, "You must eneter valid integer for port number");
			return;
		}
		
		ConnectionToServer connection = new ConnectionToServer(serverIP, serverPort, numberOfThreads, name, txtStatus);
		connection.start();
		
		lblWorkstationStatus.setText("Workstation is running");
		lblWorkstationStatus.setForeground(Color.green);
		btnRunWorkstation.setEnabled(false);
	}
}
